﻿Public Class TMyTablePrivilegesList
    Implements System.IDisposable

    Private _Owner As TMyUser
    Private _List As New List(Of TMyTablePrivileges)
    Private _SchemaList As New List(Of String)

    Public ReadOnly Property Host() As String
        Get
            Return _Owner.Host
        End Get
    End Property
    Public ReadOnly Property User() As String
        Get
            Return _Owner.UserName
        End Get
    End Property
    Public ReadOnly Property Schemas(ByVal Index As Integer) As String
        Get
            Return _SchemaList(Index)
        End Get
    End Property
    Public ReadOnly Property SchemaCount() As Integer
        Get
            Return _SchemaList.Count
        End Get
    End Property
    Public Sub New(ByVal AOwner As TMyUser)
        _Owner = AOwner
    End Sub
    Private ReadOnly Property Query() As TMyQuery
        Get
            Return _Owner.Query
        End Get
    End Property

    Default Public ReadOnly Property Items(ByVal Index As Integer) As TMyTablePrivileges
        Get
            Return _List(Index)
        End Get
    End Property

    Public ReadOnly Property Count() As Integer
        Get
            Return _List.Count
        End Get
    End Property

    Public Sub Refresh()
        _List.Clear()
        _SchemaList.Clear()
        Query.Clear()
        Query.SQL.AppendLine("select")
        Query.SQL.AppendLine(" p.Db")
        Query.SQL.AppendLine(",p.Table_name")
        Query.SQL.AppendLine(",p.Grantor")
        Query.SQL.AppendLine(",p.Timestamp")
        Query.SQL.AppendLine(",p.Table_priv")
        Query.SQL.AppendLine(",p.Column_priv")
        Query.SQL.AppendLine(",t.table_comment")
        Query.SQL.AppendLine(",t.engine")
        Query.SQL.AppendLine(",t.create_time")
        Query.SQL.AppendLine(",t.update_time")
        Query.SQL.AppendLine("from")
        Query.SQL.AppendLine(" mysql.tables_priv p")
        Query.SQL.AppendLine("inner join information_schema.tables t on(p.Db=t.table_schema and p.Table_name=t.table_name)")
        Query.SQL.AppendLine("where")
        Query.SQL.AppendLine("host=?host")
        Query.SQL.AppendLine("and user=?user")
        Query.SQL.AppendLine("order by")
        Query.SQL.AppendLine(" Db")
        Query.SQL.AppendLine(",Table_name")
        Query.DeclareAndSet("host", _Owner.Host)
        Query.DeclareAndSet("user", _Owner.UserName)
        Query.Execute()
        While Not Query.Eof
            Dim item As New TMyTablePrivileges
            item._DB = Query.FieldAsString("DB")
            item._TableName = Query.FieldAsString("Table_name")
            item._Grantor = Query.FieldAsString("Grantor")
            item._Timestamp = Query.FieldAsDateTime("Timestamp")
            item._TablePriv = Query.FieldAsString("Table_priv")
            item._ColumnPriv = Query.FieldAsString("Column_priv")
            item._TableComment = Query.FieldAsString("table_comment")
            item._Engine = Query.FieldAsString("engine")
            item._CreateTime = Query.FieldAsDateTime("create_time")
            item._UpdateTime = Query.FieldAsDateTime("update_time")
            _List.Add(item)
            If Not _SchemaList.Contains(item.DB) Then
                _SchemaList.Add(item.DB)
            End If
            Query.MoveNext()
        End While
        Query.Close()
    End Sub

    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If
            _List.Clear()
            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
